import {createRouter, createWebHistory} from 'vue-router'
import HomeView from '../views/HomeView.vue'
import LoginView from '../views/LoginView.vue'
import UserManagementView from '../views/UserManagementView.vue'
import ProductManagementView from '../views/ProductManagementView.vue'
import FieldLibraryView from '../views/FieldLibraryView.vue'
// 导入并设置路由守卫
import {setRouter} from './guard'

// 占位页面组件
const AccessPlaceholder = () => import('../views/AccessPlaceholder.vue')
const DataPlaceholder = () => import('../views/DataPlaceholder.vue')
const ProductPermissionManagementView = () => import('../views/permission/ProductPermissionManagementView.vue')

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: HomeView,
      meta: {
        title: '首页',
        roles: ['ADMIN', 'SUPER_ADMIN', 'DEVELOPER', 'VIEWER']
      }
    },
    {
      path: '/login',
      name: 'login',
      component: LoginView,
      meta: {
        title: '登录',
        guest: true // 允许游客访问
      }
    },

    // === 新的业务领域路由结构 ===

    // 用户与权限管理 (/access)
    {
      path: '/access',
      name: 'access',
      component: AccessPlaceholder,
      meta: {
        title: '用户与权限',
        roles: ['ADMIN', 'SUPER_ADMIN']
      }
    },
    {
      path: '/access/users',
      name: 'access-users',
      component: UserManagementView,
      meta: {
        title: '用户管理',
        roles: ['ADMIN', 'SUPER_ADMIN']
      }
    },
    {
      path: '/access/permissions',
      name: 'access-permissions',
      component: ProductPermissionManagementView,
      meta: {
        title: '产品权限管理',
        roles: ['ADMIN', 'SUPER_ADMIN']
      }
    },

    // 数据管理 (/data)
    {
      path: '/data',
      name: 'data',
      component: DataPlaceholder,
      meta: {
        title: '数据管理',
        roles: ['ADMIN', 'SUPER_ADMIN', 'DEVELOPER', 'VIEWER']
      }
    },
    {
      path: '/data/products',
      name: 'data-products',
      component: ProductManagementView,
      meta: {
        title: '产品管理',
        roles: ['ADMIN', 'SUPER_ADMIN', 'DEVELOPER', 'VIEWER']
      }
    },
    {
      path: '/data/field-library',
      name: 'data-field-library',
      component: FieldLibraryView,
      meta: {
        title: '字段库管理',
        roles: ['ADMIN', 'SUPER_ADMIN', 'DEVELOPER', 'VIEWER']
      }
    },

    // === 向后兼容的重定向路由 ===

    // 旧用户管理路径重定向
    {
      path: '/users',
      redirect: '/access/users'
    },
    // 旧权限管理路径重定向
    {
      path: '/permission',
      redirect: '/access'
    },
    {
      path: '/permission/product-permissions',
      redirect: '/access/permissions'
    },
    // 旧数据管理路径重定向
    {
      path: '/metadata',
      redirect: '/data'
    },
    // 旧产品管理路径重定向
    {
      path: '/products',
      redirect: '/data/products'
    },
    // 旧字段库管理路径重定向
    {
      path: '/field-library',
      redirect: '/data/field-library'
    },

    // === 错误页面 ===
    {
      path: '/403',
      name: '403',
      component: () => import('../views/ForbiddenView.vue'),
      meta: {
        title: '权限不足',
        guest: true
      }
    },
    {
      path: '/:pathMatch(.*)*',
      name: '404',
      component: () => import('../views/NotFoundView.vue'),
      meta: {
        title: '页面不存在',
        guest: true
      }
    }
  ],
})

setRouter(router)

export default router
